package br.com.intelsys.basics.action;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

/**
 * Manipula os cookies de um sistema.
 * 
 * Esta classe foi criada para encapsular a implementacao dos Servlets
 * 
 * O objeto Cookie que ela retorna e recebe nao eh o javax.servlet.http.Cookie e
 * sim o Cookie que faz parte deste mesmo pacote
 * 
 * @author patrick
 * 
 */

public class CookieList implements List<Object> {

	CookieList() {

	}

	private javax.servlet.http.Cookie[] getCookies() {
		javax.servlet.http.Cookie[] cList = ServletActionContext.getRequest().getCookies();
		if (cList == null) {
			cList = new javax.servlet.http.Cookie[0];
		}
		return (cList);
	}

	public boolean add(Object e) {
		boolean did;
		if (e instanceof Cookie) {
			ServletActionContext.getResponse().addCookie(((Cookie) e).getServletCookie());
			did = true;
		} else if (e == null) {
			did = false;
		} else {
			LogFactory.getLog(CookieList.class).error("NAO eh um com.catho.cactionCookie !");
			did = false;
		}
		return (did);
	}

	public void add(int index, Object element) {
		this.add(element);
	}

	public boolean addAll(@SuppressWarnings("rawtypes") Collection c) {
		for (Object o : c) {
			this.add(o);
		}
		return false;
	}

	public boolean addAll(int index, @SuppressWarnings("rawtypes") Collection c) {
		this.addAll(c);
		return false;
	}

	public void clear() {
		javax.servlet.http.Cookie[] cList = this.getCookies();
		for (javax.servlet.http.Cookie c : cList) {
			c.setValue(null);
		}
	}

	public boolean contains(Object o) {
		boolean retTmp = false;
		if (o instanceof String) {
			javax.servlet.http.Cookie[] cList = this.getCookies();
			for (javax.servlet.http.Cookie c : cList) {
				if (c.getName() != null && c.getName().equals((String) o)) {
					retTmp = true;
					break;
				}
			}
		}
		return (retTmp);
	}

	public boolean containsAll(@SuppressWarnings("rawtypes") Collection c) {
		boolean retTmp = false;
		for (Object o : c) {
			if (!this.contains(o)) {
				retTmp = false;
				break;
			}
		}
		return (retTmp);
	}

	public Object get(int index) {
		Cookie c;
		javax.servlet.http.Cookie[] cList = this.getCookies();
		if (cList != null && cList.length - 1 <= index) {
			c = new Cookie(cList[index]);
		} else {
			c = null;
		}
		return (c);
	}

	public int indexOf(Object o) {
		int index = -1;
		javax.servlet.http.Cookie[] cList = this.getCookies();
		for (javax.servlet.http.Cookie c : cList) {
			index++;
			if (c.getName().equals(o)) {
				break;
			}
		}
		return (index);
	}

	public boolean isEmpty() {
		return (this.getCookies() == null || this.getCookies().length == 0);
	}

	public Iterator<Object> iterator() {
		return (new CookieIterator(this.getCookies()));
	}

	public int lastIndexOf(Object o) {
		int index = -1;
		int lastIndex = -1;
		javax.servlet.http.Cookie[] cList = this.getCookies();
		for (javax.servlet.http.Cookie c : cList) {
			index++;
			if (c.getName().equals(o)) {
				lastIndex = index;
			}
		}
		return (lastIndex);
	}

	public ListIterator<Object> listIterator() {
		return null;
	}

	public ListIterator<Object> listIterator(int index) {
		return null;
	}

	public boolean remove(Object o) {
		boolean did = false;
		javax.servlet.http.Cookie[] cList = this.getCookies();
		for (javax.servlet.http.Cookie c : cList) {
			if (c.getName().equals(o)) {
				c.setValue(null);
				did = true;
				break;
			}
		}
		return (did);
	}

	public Object remove(int index) {
		int i = -1;
		Cookie cTmp = null;
		javax.servlet.http.Cookie[] cList = this.getCookies();
		for (javax.servlet.http.Cookie c : cList) {
			if (i++ == index) {
				c.setValue(null);
				cTmp = new Cookie(c);
				break;
			}
		}
		return (cTmp);
	}

	public boolean removeAll(@SuppressWarnings("rawtypes") Collection c) {
		boolean retTmp = false;
		for (Object o : c) {
			if (this.remove(o)) {
				retTmp = true;
			}
		}
		return (retTmp);
	}

	public boolean retainAll(@SuppressWarnings("rawtypes") Collection c) {
		boolean did = false;
		javax.servlet.http.Cookie[] cList = this.getCookies();
		for (javax.servlet.http.Cookie cookie : cList) {
			if (!c.contains(cookie.getName())) {
				cookie.setValue(null);
				did = true;
			}
		}
		return (did);
	}

	public Object set(int index, Object element) {
		this.remove(index);
		this.add(element);
		return null;
	}

	public int size() {
		return (this.getCookies().length);
	}

	public List<Object> subList(int fromIndex, int toIndex) {
		return null;
	}

	public Object[] toArray() {
		return (this.getCookies());
	}

	@SuppressWarnings("unchecked")
	public Object[] toArray(Object[] a) {
		return (this.getCookies());
	}

}
